package com.meixiang.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.services.Evaluate;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 评价
 * author:xiejingwen
 */
public class EvaluateView extends LinearLayout {
    private Context mContext;
    private List<Evaluate> data;
    public final static int STYLE_SERVICE = 0;//美服务样式
    public final static int STYLE_SHOPPING = 1;//美商城样式
    private int style;//样式
    private int resId = R.drawable.shape_green_progress;

    public EvaluateView(Context context) {
        this(context, null);
    }

    public EvaluateView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public EvaluateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setOrientation(VERTICAL);
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.evaluate, defStyleAttr, 0);
        style = array.getInt(R.styleable.evaluate_style, STYLE_SERVICE);
        data = new ArrayList<>();
        //虚拟数据
       /* data.add(new Evaluate("001","性价比高","10"));
        data.add(new Evaluate("002","使用体验","90"));
        data.add(new Evaluate("001","发货速度","60"));
        data.add(new Evaluate("001", "包装很好", "100"));*/
        setStyle(style);
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void setData(List<Evaluate> data) {
        if (data == null) {
            return;
        }
        this.data.clear();
        for (int i = 0; i < data.size(); i++) {
            if (AbStrUtil.isEmpty(data.get(i).getTagCount())) {
                data.get(i).setTagCount("0");
            }
        }
        this.data.addAll(data);
        initView();
    }

    /**
     * 传入0（美服务样式）
     * 传入1（美商场样式）
     *
     * @param style
     */
    public void setStyle(int style) {
        this.style = style;
        if (this.style == STYLE_SHOPPING) {
            resId = R.drawable.shape_red_progress;
        } else {
            resId = R.drawable.shape_green_progress;
        }
        initView();
    }

    private void initView() {
        removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            LinearLayout layout = new LinearLayout(mContext);
            LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                params.topMargin = DensityUtil.dip2px(mContext, 15);
            }
            layout.setLayoutParams(params);
            layout.setOrientation(HORIZONTAL);
            layout.setGravity(Gravity.CENTER_VERTICAL);
            TextView leftText = new TextView(mContext);
            leftText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            leftText.setText(data.get(i).getTagName() + "：");
            leftText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            leftText.setTextColor(ContextCompat.getColor(mContext, R.color.black_9));
            layout.addView(leftText);
            View view = new View(mContext);
            //LinearLayout.LayoutParams vParams=new LinearLayout.LayoutParams(singe*Integer.parseInt(data.get(i).getTagCount()),DensityUtil.dip2px(mContext,12));
            view.setLayoutParams(new LinearLayout.LayoutParams(0, DensityUtil.dip2px(mContext, 12), 1));
            view.setBackgroundResource(resId);
            layout.addView(view);
            TextView rightText = new TextView(mContext);
            rightText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            rightText.setText(data.get(i).getTagCount());
            rightText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            rightText.setTextColor(ContextCompat.getColor(mContext, R.color.black_6));
            rightText.setPadding(DensityUtil.dip2px(mContext, 4), 0, 0, 0);
            layout.addView(rightText);
            addView(layout);
        }
        changWidth();
    }

    //修改所有左边TextView的宽度为最大TextView的宽度
    private void changWidth() {
        post(new Runnable() {
            @Override
            public void run() {
                int maxTextWidth = 0;//左边文字的宽度
                int singe = 0;//每一份的宽度
                int maxWidth = 0; //最大宽度 dp
                for (int i = 0; i < getChildCount(); i++) {
                    LinearLayout layout = (LinearLayout) getChildAt(i);
                    if (i==0){
                        maxWidth=layout.getChildAt(1).getWidth();
                    }
                    int width = layout.getChildAt(0).getWidth();
                    if (maxTextWidth < width) {
                        maxTextWidth = width;
                    }
                    if (maxWidth>layout.getChildAt(1).getWidth()){
                        maxWidth=layout.getChildAt(1).getWidth();
                    }
                }
                int maxCount = 0;
                for (int i = 0; i < data.size(); i++) {
                    if ( Integer.parseInt(data.get(i).getTagCount())> maxCount) {
                        maxCount = Integer.parseInt(data.get(i).getTagCount());
                    }
                }
                if (maxCount>0){
                    singe = maxWidth / maxCount;
                }
                for (int i = 0; i < getChildCount(); i++) {
                    LinearLayout layout = (LinearLayout) getChildAt(i);
                    layout.getChildAt(0).setLayoutParams(new LayoutParams(maxTextWidth, LinearLayout.LayoutParams.WRAP_CONTENT));
                    LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(singe * Integer.parseInt(data.get(i).getTagCount()), DensityUtil.dip2px(mContext, 12));
                    layout.getChildAt(1).setLayoutParams(vParams);
                }
            }
        });
    }
}
